package com.go.where.manager.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.system.MenuDao;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.service.system.MenuBuildService;
import com.whalin.MemCached.MemCachedClient;

@SuppressWarnings("unchecked")
@Service("menuBuildService")
public class MenuBuildServiceImpl implements MenuBuildService {
	private static Logger log = Logger.getLogger(MenuBuildService.class);
	@Resource(name = "memcachedClient")
	private MemCachedClient memcachedClient;
	@Resource(name = "menuDao")
	private MenuDao menuDao;

	public final boolean builMenu() {
		List<Menu> list1 = build(Constants.MENU1);
		Map<String, Menu> map = new HashMap<String, Menu>();
		if (list1 != null) {
			for (Menu p : list1) {
				map.put(p.getMenuId(), p);
			}
		}
		boolean b = memcachedClient.set(Constants.MENU1, map);
		log.info("加载一级菜单:" + b);

		b = memcachedClient.set(Constants.MENU2, build(Constants.MENU2));
		log.info("加载二级菜单:" + b);

		b = memcachedClient.set(Constants.MENU3, build(Constants.MENU3));
		log.info("加载三级菜单:" + b);
		rightsMenu();
		return b;
	}

	public final boolean rightsMenu() {
		Object menu = memcachedClient.get(Constants.MENU1);
		// 没有数据的时候重新获取数据库表的菜单
		if (menu == null)
			this.builMenu();
		if (menu != null && menu instanceof HashMap) {
			Map<String, Menu> map = (HashMap<String, Menu>) menu;
			Iterator<Entry<String, Menu>> st = map.entrySet().iterator();
			while (st.hasNext()) {
				Entry<String, Menu> entry = st.next();
				Menu permission = entry.getValue();
				memcachedClient.set(Constants.MENU_MEM_TIP
						+ permission.getMenuUrl(), permission.getMenuId());
			}
		}
		log.info("加载一级菜单:" + menu);
		menu = memcachedClient.get(Constants.MENU2);
		if (menu != null && menu instanceof ArrayList) {
			List<Menu> list = (ArrayList<Menu>) menu;
			Iterator<Menu> st = list.iterator();
			while (st.hasNext()) {
				Menu permission = st.next();
				memcachedClient.set(Constants.MENU_MEM_TIP
						+ permission.getMenuUrl(), permission.getMenuId());
			}
		}
		log.info("加载二级菜单:" + menu);
		menu = memcachedClient.get(Constants.MENU3);
		if (menu != null && menu instanceof ArrayList) {
			List<Menu> list = (ArrayList<Menu>) menu;
			Iterator<Menu> st = list.iterator();
			while (st.hasNext()) {
				Menu permission = st.next();
				memcachedClient.set(Constants.MENU_MEM_TIP
						+ permission.getMenuUrl(), permission.getMenuId());
			}
		}
		log.info("加载三级菜单:" + menu);
		return true;
	}

	@Override
	public final String getCode(String url) {
		url = url.trim();
		Object ob = this.memcachedClient.get(Constants.MENU_MEM_TIP + url);
		if (ob != null)
			return ob.toString();
		return null;
	}

	@Override
	public List<Menu> getAllMenu() {
		Map<String, Menu> map = (Map<String, Menu>) memcachedClient
				.get(Constants.MENU1);
		List<Menu> list1 = new ArrayList<Menu>();
		list1.addAll(map.values());
		List<Menu> list2 = (List<Menu>) memcachedClient
				.get(Constants.MENU2);
		List<Menu> list3 = (List<Menu>) memcachedClient
				.get(Constants.MENU3);

		List<Menu> all = new ArrayList<Menu>();
		for (Menu m1 : list1) {
			all.add(m1);
			for (Menu m2 : list2) {
				if (m1.getMenuId().equals(m2.getMenuParent())) {
					all.add(m2);
					for (Menu m3 : list3) {
						if (m2.getMenuId().equals(m3.getMenuParent())) {
							all.add(m3);
						}
					}
				}
			}
		}
		return all;
	}

	/**
	 * 根据菜单级别查询出该级别下的所有菜单
	 * 
	 * @param menuLv
	 * @return
	 */
	private List<Menu> build(String menuLv) {
		List<Menu> list = new ArrayList<Menu>();
		if (Constants.MENU1.equals(menuLv)) {
			list = this.menuDao.query1LevelMenu();
		} else if (Constants.MENU2.equals(menuLv)) {
			list = this.menuDao.query2LevelMenu();
		} else if (Constants.MENU3.equals(menuLv)) {
			list = this.menuDao.query3LevelMenu();
		}

		return list;
	}
}
